package cn.edu.scau.cmi.zhangjiankun.comprehension.client;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.edu.scau.cmi.zhangjiankun.comprehension.dao.studentDao;
import cn.edu.scau.cmi.zhangjiankun.comprehension.dao.teacherDao;
import cn.edu.scau.cmi.zhangjiankun.comprehension.domain.Student;
import cn.edu.scau.cmi.zhangjiankun.comprehension.domain.Teacher;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class Controller {
	@FXML
	private TextField inputField0;
	@FXML
    private TextField inputField1;
	@FXML
	private TextField inputField2;
	@FXML
	private TextField inputField3;
	@FXML
	private TextField inputField4;
	@FXML
	private TextField inputField5;
	@FXML
	private TextArea outputArea;
	private List<Student> list = new ArrayList<>();
	private List<Teacher> list2 = new ArrayList<>();
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	@FXML
	private void initialize() {
    }
	
	@FXML
    private void studentAdd() throws IOException {
		InputStream inputStream = Resources.getResourceAsStream("Mybatis-conf.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = factory.openSession();
		studentDao sDao = session.getMapper(studentDao.class);
		Student student = Student.getStudent();
		student.setName(inputField1.getText());
		student.setNumber(Long.valueOf(inputField2.getText()));
		student.setTutor(Long.valueOf(inputField3.getText()));
		sDao.add(student);
		list=sDao.getAll();
        outputArea.clear();
        for(Student o:list) {
            outputArea.appendText(o.getId()+" "+o.getName()+"  "+o.getNumber()+"  "+o.getTutor()+"\n");
        }
        session.commit();
		session.close();
		inputStream.close();
    }
	
	@FXML
    private void studentUpdate() throws IOException{
		InputStream inputStream = Resources.getResourceAsStream("Mybatis-conf.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = factory.openSession();
		studentDao sDao = session.getMapper(studentDao.class);
		Student student = Student.getStudent();
		student.setId(Long.valueOf(inputField0.getText()));
		student.setName(inputField1.getText());
		student.setNumber(Long.valueOf(inputField2.getText()));
		student.setTutor(Long.valueOf(inputField3.getText()));
		sDao.update(student);
		list=sDao.getAll();
        outputArea.clear();
        for(Student o:list) {
            outputArea.appendText(o.getId()+" "+o.getName()+"  "+o.getNumber()+"  "+o.getTutor()+"\n");        
        }
        session.commit();
		session.close();
		inputStream.close();
    }
	
	@FXML
    private void studentDelete() throws IOException{
		InputStream inputStream = Resources.getResourceAsStream("Mybatis-conf.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = factory.openSession();
		studentDao sDao = session.getMapper(studentDao.class);
		sDao.delete(Long.valueOf(inputField0.getText()));
		list=sDao.getAll();
        outputArea.clear();
        for(Student o:list) {
            outputArea.appendText(o.getId()+" "+o.getName()+"  "+o.getNumber()+"  "+o.getTutor()+"\n");
        }
        session.commit();
		session.close();
		inputStream.close();
    }
	
	@FXML
	private void teacherAdd() throws IOException{
		InputStream inputStream = Resources.getResourceAsStream("Mybatis-conf.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = factory.openSession();
		teacherDao tDao = session.getMapper(teacherDao.class);
		Teacher teacher = (Teacher)applicationContext.getBean("teacher");
		teacher.setId(Long.valueOf(inputField4.getText()));
		teacher.setName(inputField5.getText());
		tDao.add(teacher);
		list2=tDao.getAll();
        outputArea.clear();
        for(Teacher o:list2) {
            outputArea.appendText(o.getId()+"  "+o.getName()+"\n");
        }
        session.commit();
		session.close();
		inputStream.close();
	}
	
	@FXML
	private void teacherUpdate() throws IOException{
		InputStream inputStream = Resources.getResourceAsStream("Mybatis-conf.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = factory.openSession();
		teacherDao tDao = session.getMapper(teacherDao.class);
		Teacher teacher = (Teacher)applicationContext.getBean("teacher");
		teacher.setId(Long.valueOf(inputField4.getText()));
		teacher.setName(inputField5.getText());
		tDao.update(teacher);
		list2=tDao.getAll();
        outputArea.clear();
        for(Teacher o:list2) {
            outputArea.appendText(o.getId()+"  "+o.getName()+"\n");
        }
        session.commit();
		session.close();
		inputStream.close();
	}
	
	
	@FXML
	private void teacherDelete() throws IOException{
		InputStream inputStream = Resources.getResourceAsStream("Mybatis-conf.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = factory.openSession();
		teacherDao tDao = session.getMapper(teacherDao.class);
		tDao.delete(Long.valueOf(inputField4.getText()));
		list2=tDao.getAll();
        outputArea.clear();
        for(Teacher o:list2) {
            outputArea.appendText(o.getId()+"  "+o.getName()+"\n");
        }
        session.commit();
		session.close();
		inputStream.close();
	}
	
	@FXML
	private void getAllTeacher() throws IOException{
		InputStream inputStream = Resources.getResourceAsStream("Mybatis-conf.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = factory.openSession();
		teacherDao tDao = session.getMapper(teacherDao.class);
		list2=tDao.getAll();
        outputArea.clear();
        for(Teacher o:list2) {
            outputArea.appendText(o.getId()+"  "+o.getName()+"\n");
        }
        session.commit();
		session.close();
		inputStream.close();
	}
	
	@FXML
	private void getAllStudent() throws IOException{
		InputStream inputStream = Resources.getResourceAsStream("Mybatis-conf.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = factory.openSession();
		studentDao sDao = session.getMapper(studentDao.class);
		list=sDao.getAll();
        outputArea.clear();
        for(Student o:list) {
            outputArea.appendText(o.getId()+" "+o.getName()+"  "+o.getNumber()+"  "+o.getTutor()+"\n");
        }
        session.commit();
		session.close();
		inputStream.close();
	}
	
}
